<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <h2 class="login-title">系统登录</h2>
        <div class="login-subtitle">欢迎使用管理系统</div>
      </div>

      <el-form
          ref="loginForm"
          :model="loginData"
          :rules="loginRules"
          @submit.prevent="handleLogin"
          class="login-form"
      >
        <el-form-item prop="username">
          <el-input
              v-model="loginData.username"
              placeholder="请输入用户名"
              prefix-icon="User"
              size="large"
              clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="loginData.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              size="large"
              show-password
              @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              size="large"
              class="login-btn"
              :loading="loading"
              native-type="submit"
          >
            登 录
          </el-button>
        </el-form-item>

        <el-alert
            v-if="error"
            :title="error"
            type="error"
            show-icon
            :closable="false"
            class="error-message"
        />
      </el-form>

    </el-card>

    <div class="copyright">
      © 2023 公司名称 版权所有
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive, onMounted} from 'vue'
import {useRouter} from 'vue-router'
import {useAuthStore} from '../stores/auth'
import {ElMessage} from 'element-plus'

interface LoginForm {
  username: string
  password: string
}

const loginData = reactive<LoginForm>({
  username: 'admin',
  password: '123456'
})

const loginRules = reactive({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
  ]
})

const error = ref('')
const loading = ref(false)
const rememberMe = ref(false)
const router = useRouter()
const authStore = useAuthStore()

const handleLogin = async () => {
  try {
    loading.value = true
    error.value = ''

    const response = await login(loginData.username, loginData.password)
    authStore.login(response.token)

    // 记住我功能
    if (rememberMe.value) {
      localStorage.setItem('rememberedUser', loginData.username)
    } else {
      localStorage.removeItem('rememberedUser')
    }

    ElMessage.success('登录成功')
    window.electronAPI.invoke('resize-window', {width: 1920, height: 1080})
    // 获取重定向路径
    const redirectPath = router.currentRoute.value.query.redirect || '/main'
    await router.push(redirectPath);
  } catch (err) {
    error.value = '登录失败: ' + (err as Error).message
  } finally {
    loading.value = false
  }
}

// 模拟登录接口
const login = async (username: string, password: string) => {
  const response = await window.electronAPI.invoke('login', {username: username, password: password});
  if (response.success) {
    return {token: response.data.token};
  } else {
    throw new Error(response.message);
  }
}

// 记住我功能 - 初始化时检查
onMounted(() => {
  const rememberedUser = localStorage.getItem('rememberedUser')
  if (rememberedUser) {
    loginData.username = rememberedUser
    rememberMe.value = true
  }
})
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
  border: none;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 24px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.login-subtitle {
  font-size: 14px;
  color: #888;
}

.login-form {
  margin-top: 20px;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
  letter-spacing: 2px;
}

.error-message {
  margin-top: 10px;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  font-size: 14px;
}

.copyright {
  margin-top: 30px;
  font-size: 12px;
  color: #999;
  text-align: center;
}

:deep(.el-card__body) {
  padding: 40px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
}
</style>